package com.ruoyi.meter.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.meter.domain.MeterLedger2;
import com.ruoyi.meter.service.impl.MeterLedgerServiceImpl;
import com.ruoyi.system.service.ISysUserService;

import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.meter.domain.MeterLedger;
import com.ruoyi.meter.service.IMeterLedgerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 计量台账Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/meter/ledger")
public class MeterLedgerController extends BaseController
{
    @Autowired
    private IMeterLedgerService meterLedgerService;


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MeterLedger> util = new ExcelUtil<MeterLedger>(MeterLedger.class);
        util.importTemplateExcel(response, "台账数据");
    }

    /**
     * 查询计量台账列表
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeterLedger meterLedger)
    {
        startPage();
        List<MeterLedger> list = meterLedgerService.selectMeterLedgerList(meterLedger);
        return getDataTable(list);
    }

    /**
     * 导出计量台账列表
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:export')")
    @Log(title = "计量台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeterLedger meterLedger)
    {
        List<MeterLedger> list = meterLedgerService.selectMeterLedgerList(meterLedger);
        ExcelUtil<MeterLedger> util = new ExcelUtil<MeterLedger>(MeterLedger.class);
        util.exportExcel(response, list, "计量台账数据");
    }
    /**
     * 导出计量计划列表
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:exportplan')")
    @Log(title = "计量台账", businessType = BusinessType.EXPORT)
    @PostMapping("/exportplan")
    public void exportPlan(HttpServletResponse response, MeterLedger meterLedger)
    {
        List<MeterLedger> list = meterLedgerService.selectMeterLedgerList(meterLedger);
        List<MeterLedger2> list2 = new ArrayList();
        list.forEach(meterLedger1 -> {
            MeterLedger2 meterLedger2=new MeterLedger2();
            BeanUtils.copyProperties(meterLedger1,meterLedger2);
            list2.add(meterLedger2);
        });
        BeanUtils.copyProperties(list, list2);
        ExcelUtil<MeterLedger2> util = new ExcelUtil<MeterLedger2>(MeterLedger2.class);
        util.exportExcel(response, list2, "计量台账数据");
    }

    /**
     * 导入计量台账列表
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('meter:ledger:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<MeterLedger> util = new ExcelUtil<MeterLedger>(MeterLedger.class);
        List<MeterLedger> meterLedgerList = util.importExcel(file.getInputStream());
        String operName = getUsername();

        String message = meterLedgerService.importUser(meterLedgerList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 获取计量台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return AjaxResult.success(meterLedgerService.selectMeterLedgerByProjectId(projectId));
    }

    /**
     * 获取计量台账分类统计数据
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:list')")
    @GetMapping("/listCount")
    public AjaxResult selectMeterLedgerCountList(MeterLedger meterLedger)
    {
        return AjaxResult.success(meterLedgerService.selectMeterLedgerCountList(meterLedger));
    }



    /**
     * 新增计量台账
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:add')")
    @Log(title = "计量台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeterLedger meterLedger)
    {
        meterLedgerService.setPerfect(meterLedger);
        return toAjax(meterLedgerService.insertMeterLedger(meterLedger));
    }

    /**
     * 修改计量台账
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:edit')")
    @Log(title = "计量台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeterLedger meterLedger)
    {
        meterLedgerService.setPerfect(meterLedger);
        return toAjax(meterLedgerService.updateMeterLedger(meterLedger));
    }

    /**
     * 删除计量台账
     */
    @PreAuthorize("@ss.hasPermi('meter:ledger:remove')")
    @Log(title = "计量台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(meterLedgerService.deleteMeterLedgerByProjectIds(projectIds));
    }
}
